//
// Created by sjjmw on 3/24/2022.
//

//应该使用dfs

#include "iostream"
#include "vector"
#include "list"
#include "algorithm"
#include "string"
#include "unordered_map"
#include "cmath"
#include<iomanip>
#include "string"
#include "map"
#include "list"
#include "queue"

struct State{
    std::string str;
    int depth=0;
};
struct Sol{
    std::queue<State> queue;
    std::unordered_map<std::string,char> appear;
    void handle(
            int n1
            ){
        State s;
        std::getline(std::cin,s.str);
        std::getline(std::cin,s.str);
        queue.push(s);
        while (!queue.empty()){
            auto&state=queue.front();
            if(appear.find(state.str)!=appear.end()){
                queue.pop();
                continue;
            }
            appear[state.str]=1;
            int find=state.str.find("2012");
            if(find!=-1){
                //找到了2012
                printf("%d\n",state.depth);
                return;
            }
            for(int i=0;i<state.str.size()-1;i++){
                if(state.str[i]!=state.str[i+1]){
                    auto cpy=state;
                    std::swap(cpy.str[i],cpy.str[i+1]);
                    cpy.depth++;

                    queue.push(cpy);
                }
            }
            queue.pop();
        }
        printf("-1\n");
    }
};
int main(){
    int a,b,c;
//    float a,b,c,d;
    int e;

    while(std::cin>>a){
        Sol().handle(a);
//        Sol().handle(a,b);
    }

    return 0;
}